<?php

/**
 * 画像操作类
 * gcImageMagickService
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */

class gcImageMagickService extends gcComBasicService{
    /**
     * __construct
     */
    public function __construct(){
       parent::__construct();
    }

    /**
     * changeImageSize
     * 画像尺寸变更
     * @param   string  $srcFile
     * @param   array   $arrProperty
     * @param   string  $savePath
     * @param   string  $trim
     * @return  bool
     */
    public function changeImageSize($srcFile, $arrProperty, $savePath, $trim=false){
        if(count($arrProperty) > 0){
            for($i = 0; $i < count($arrProperty); $i++){
                $new_w = intval($arrProperty[$i]["width"]);
                $new_h = intval($arrProperty[$i]["height"]);
                $destFile = $savePath . "/" . $arrProperty[$i]["fileName"];

                if($new_w <= 0 || $new_h <= 0 || !file_exists($srcFile)){
                    return false;
                }
                $src = new Imagick($srcFile);
                $image_format = strtolower($src->getImageFormat());
                if($image_format != 'jpeg' && $image_format != 'gif' && $image_format != 'png' && $image_format != 'jpg'){
                    return false;
                }
                $src_page = $src->getImagePage();
                
                if($image_format != 'gif'){
                    $dest = $src;
                    if(!$trim){
                        $dest->thumbnailImage($new_w, $new_h, true);
                    }else{
                        $dest->cropthumbnailImage($new_w, $new_h);
                    }
                    $dest->writeImage($destFile);
                    $dest->clear();
                }else{
                    $dest = new Imagick();
                    $color_transparent = new ImagickPixel("transparent");
                    foreach($src as $img){
                        $page = $img->getImagePage();
                        $tmp = new Imagick();
                        $tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
                        $tmp->compositeImage($img, Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
                        if(!$trim){
                            $tmp->thumbnailImage($new_w, $new_h, true);
                        }else{
                            $tmp->cropthumbnailImage($new_w, $new_h);
                        }
                        $dest->addImage($tmp);
                        $dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
                        $dest->setImageDelay($img->getImageDelay());
                        $dest->setImageDispose($img->getImageDispose());
                    }
                    $dest->coalesceImages();
                    $dest->writeImages($destFile, true);
                    $dest->clear();
                }
            }
            return true;
        }else{
            return false;
        }
    }
}